package edu.ccut.saturn.studio.dialog.iterator;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnEndIterationNode;

public  class SaturnEndIteratorDialog extends TitleAreaDialog {

	private Combo startIteratorIdCombo;
	
	private Text idText, nameText, descriptionText;
	
	private Label idLabel, nameLabel, descriptionLabel;
	
	private SaturnEndIterationNode saturnEndIterationNode;
	
	public SaturnEndIteratorDialog(Shell parentShell,SaturnEndIterationNode saturnViewerNode) {
		super(parentShell);
		this.saturnEndIterationNode = saturnViewerNode;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		setMessage(SelectionCandidate.SETTING_ATTRIBUTE + saturnEndIterationNode.getName().trim());
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME)); //$NON-NLS-1$
		
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		idLabel = new Label(container, SWT.NONE);
		final GridData gd_idLabel = new GridData();
		gd_idLabel.verticalIndent = 15;
		idLabel.setLayoutData(gd_idLabel);
		idLabel.setText(Messages.getString("SaturnEndIteratorDialog.ID")); //$NON-NLS-1$
		
		idText = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		idText.setTextLimit(50);
		final GridData gd_textId = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textId.horizontalIndent = 5;
		gd_textId.verticalIndent = 5;
		idText.setLayoutData(gd_textId);
		idText.setText(saturnEndIterationNode.getId().trim());
		idText.setSelection(saturnEndIterationNode.getId().trim().length());

		nameLabel = new Label(container, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 15;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("SaturnEndIteratorDialog.name"));   //$NON-NLS-1$
		
		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		final GridData gd_textName = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textName.horizontalIndent = 5;
		gd_textName.verticalIndent = 5;
		nameText.setLayoutData(gd_textName);
		nameText.setText(saturnEndIterationNode.getName().trim());
		nameText.setSelection(saturnEndIterationNode.getName().trim().length());

		final Label label_1 = new Label(container, SWT.NONE);
		label_1.setText(Messages.getString("SaturnEndIteratorDialog.IteratorStart")); //$NON-NLS-1$

		startIteratorIdCombo = new Combo(container, SWT.READ_ONLY);
		startIteratorIdCombo.setItems(saturnEndIterationNode.getStartIteratorIds());
		startIteratorIdCombo.setText(saturnEndIterationNode.getStartid());
		final GridData gd_endIteratorIdCombo = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_endIteratorIdCombo.verticalIndent = 5;
		gd_endIteratorIdCombo.horizontalIndent = 5;
		startIteratorIdCombo.setLayoutData(gd_endIteratorIdCombo);
		
		descriptionLabel = new Label(container, SWT.NONE);
		final GridData gd_descriptionLabel = new GridData(SWT.LEFT, SWT.TOP, false, false);
		gd_descriptionLabel.verticalIndent = 15;
		descriptionLabel.setLayoutData(gd_descriptionLabel);
		descriptionLabel.setText(Messages.getString("SaturnEndIteratorDialog.description"));   //$NON-NLS-1$
		
		descriptionText = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		descriptionText.setTextLimit(200);
		final GridData gd_textDec = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textDec.heightHint = 53;
		gd_textDec.horizontalIndent = 5;
		gd_textDec.verticalIndent = 5;
		descriptionText.setLayoutData(gd_textDec);
		descriptionText.setText(saturnEndIterationNode.getDescription());
		
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if("".equals(nameText.getText().trim())){ 
					setErrorMessage(Messages.getString("SaturnEndIteratorDialog.NameLimit"));  //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				}else{
					setErrorMessage(null);
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		});
		
		return area;
	}
	
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			saturnEndIterationNode.setDescription(descriptionText.getText().trim());
			saturnEndIterationNode.setName(nameText.getText().trim());
			saturnEndIterationNode.setStartid(startIteratorIdCombo.getText().trim());
		}
			
		super.buttonPressed(buttonId);
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(true);
	}
	
	@Override
	protected Point getInitialSize() {
		return new Point(500, 324);
	}
	
	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME)); //$NON-NLS-1$
	}
}